<template>
  <el-form ref="form" :model="userInfo" :rules="rules" label-width="85px">
    <el-form-item label="用户姓名" prop="realName">
      <el-input v-model="userInfo.realName"/>
    </el-form-item>

    <el-form-item label="用户性别" prop="genderCode">
      <el-select v-model="userInfo.genderCode" placeholder="请选择"  style="width: 100%;">
        <el-option
          v-for="item in genderOptions"
          :key="item.dictCode"
          :label="item.dictName"
          :value="item.dictCode"
        />
      </el-select>
    </el-form-item>

    <el-form-item label="手机号码" prop="mobileNumber">
      <el-input v-model="userInfo.mobileNumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="个性签名" prop="motto">
      <el-input v-model="userInfo.motto" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import { getDicListByDictCodeList } from '@/api/common/dict'
  import { changeCurrentLoginUserInfo } from "@/api/common/currentLoginUser";
  import { MessageBox } from 'element-ui'
  export default {
  props: {
    userInfo: {
      type: Object
    }
  },
  data() {
    return {
      //性别选项
      genderOptions:[],
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        genderCode: [
          { required: true, message: "用户性别必须选择"}
        ],
        mobileNumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { max: 11, message: "手机号码不得超过11位字符", trigger: "blur" },
          { validator: this.formValidate.mobile,trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.initDict();
  },
  methods: {
    //获取数据字典
    initDict(){
      let dictArr = ["GENDER"];
      getDicListByDictCodeList(dictArr).then(res => {
        this.genderOptions = JSON.parse(JSON.stringify(res.data['GENDER']));
      });
    },
    //提交
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          changeCurrentLoginUserInfo(this.userInfo).then(response => {
            MessageBox.alert(
              response.message,
              '系统提示',
              {
                confirmButtonText: '重新登录',
                type: 'success'
              }
            ).then(() => {
              this.$store.dispatch('FedLogOut').then(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
            })
          });
        }
      });
    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
